# coding=utf-8
import uuid
from datetime import datetime
from django.db import models

# Create your models here.


class OrderForm(models.Model):

    ORDER_STATUS = (
        (0, "CREATED"),
        (1, "PAID"),
        (2, "ACCEPTED"),
        (3, "REJECTED"),
        (4, "DONE"),
        (5, "CANCEL_NO_REFUND"),
        (6, "CANCEL_REFUNDED"),
    )

    id = models.UUIDField("编号", primary_key=True, default=uuid.uuid4, editable=False)
    consumer = models.ForeignKey("consumer.Consumer", models.CASCADE)
    seller = models.ForeignKey("shop.Seller", models.CASCADE)
    status = models.IntegerField("状态", null=False, choices=ORDER_STATUS, default=0)
    money_sum = models.BigIntegerField("总金额", null=False)
    remark = models.CharField("备注", max_length=210, null=False, blank=True, default="")
    create_time = models.DateTimeField("创建时间", auto_now_add=True);
    complate_time = models.DateTimeField("完成时间", null=True, default=None)

    def __str__(self):
        return "Consumer:{0}(Shop:{1})".format(self.consumer.name, self.seller.name)

    class Meta:
        ordering = ["-create_time"]
        verbose_name = "订单"
        verbose_name_plural = "订单"


class FoodPlate(models.Model):
    id = models.UUIDField("编号", primary_key=True, default=uuid.uuid4, editable=False)
    order = models.ForeignKey("OrderForm", models.CASCADE)
    food = models.ForeignKey("shop.Food", models.SET_NULL, null=True)
    consumer = models.ForeignKey("consumer.Consumer", models.SET_NULL, null=True)
    amount = models.SmallIntegerField("数量", null=False)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)
    complate_time = models.DateTimeField("完成时间", null=True, default=None)
    money_sum = models.BigIntegerField("金额", null=False)
    pick_num = models.SmallIntegerField("取餐号", null=True, default=None)

    def __str__(self):
        return "Food:{0}, Amount:{1}".format(self.food.name, self.amount)

    class Meta:
        ordering = ["-create_time"]
        verbose_name = "餐盘"
        verbose_name_plural = "餐盘"


class Comment(models.Model):
    id = models.UUIDField("编号", primary_key=True, default=uuid.uuid4, editable=False)
    order = models.OneToOneField("OrderForm", models.SET_NULL, null=True)
    consumer = models.ForeignKey("consumer.Consumer", models.SET_NULL, null=True)
    seller = models.ForeignKey("shop.Seller", models.CASCADE)
    grade = models.SmallIntegerField("评分", null=True, default=None)
    content = models.CharField("评价内容", max_length=210, null=False, default="")
    update_time = models.DateTimeField("最后更新时间", auto_now=True)

    def __str__(self):
        return self.content[:10]

    class Meta:
        ordering = ["-update_time"]
        verbose_name = "评论"
        verbose_name_plural = "评论"


class PickNumberPool(models.Model):
    seller = models.OneToOneField("shop.Seller", models.CASCADE, primary_key=True, verbose_name="店家")
    last_pick_time = models.DateTimeField("最后取餐时间", null=False, default=datetime(2000, 1, 1))
    last_pick_number = models.IntegerField("最后取餐编号", null=False, default=0)

    def __str__(self):
        return "{0}的取餐编号池".format(self.seller.name)

    class Meta:
        verbose_name = "取餐编号池"
        verbose_name_plural = "取餐编号池"
